import { createRouter, createWebHashHistory } from "vue-router";
import baseRoutes from "./baseRoutes"
import sysRoutes from "./sysRoutes"
const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login.vue"),
    meta: {
      title: "登录",
    }
  },
  {
    path: "/:catchAll(.*)",
    // name: "NotFound",//不能有，4.0以上不需要手动push404页面，直接这里这样配置404
    meta: {
      title: "NotFound",
    },
    component: () => import("@/views/404.vue"),
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes: [...routes, ...baseRoutes,...sysRoutes],  
});
// 路由白名单
const whiteList = ["/login", "/register"];
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (token) {
    if (to.path == '/login') {
      next()
    }
    next()
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router;
